package com.ars.common.filter;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // 初始化方法，如果需要可以在该方法中读取配置
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {

        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;

        // 检查session中是否存在用户信息
        Object user = httpRequest.getSession().getAttribute("LogUser");
        if (user == null) {
            // 如果未登录，重定向到登录页面
            httpResponse.sendRedirect(httpRequest.getContextPath() + "/src/login.jsp");
        } else {
            // 已登录，继续执行请求链
            chain.doFilter(request, response);
        }
    }

    @Override
    public void destroy() {

    }
}
